#include "../includes/mybtree.h"

static void mybtree_test_handler(mybtree *t, mybnode *n, int idx);

int main(int argc, char **argv) {
    printf("start mybtree_test\n");
    char *name = "mybtree_test";
    mybtree *t = mybtree_create(name);
    printf("after mybtree_create, btree size: %d, node size: %d\n", t->keys_num, t->nodes_num);

    if(t->keys_num == 0) {
        for (int i = 0; i < MYBTREE_KEYS_NUM_MAX * 10; i++)
        {
            uint8_t *d = calloc(1, 256);
            int *d_int = (int *) d;
            *d_int = i;
            mybtree_insert(t, i, d);
        }
        mybtree_flush_disk(t);
        printf("after mybtree_flush_disk\n");
    } else {
        int idx = 0;
        int key = 1024;
        mybtree_travel(t, t->root, mybtree_test_handler, &idx);
        void * data = mybtree_find(t, key);
        printf("before delete find key: %d, data: %d\n", key, *(int *)data);

        mybtree_delete(t, key);
        data = mybtree_find(t, key);
        if(!data) {
            printf("after delete can't find key: %d\n", key);
        } else {
            printf("after delete find key: %d, data: %d\n", key, *(int *)data);
        }
        printf("after delete:\n");
        mybtree_travel(t, t->root, mybtree_test_handler, &idx);
    }
    return 0;
}

static void mybtree_test_handler(mybtree *t, mybnode *n, int idx) {
    printf("[travel] node idx: %d, leaf: %d, node size: %d, keys: %d, %d, %d, childs: %d, %d, %d\n",
            n->node_idx, n->leaf, n->keys_num, n->keys[0], n->keys[1], n->keys[2], 
            n->children_idxs[0], n->children_idxs[1], n->children_idxs[2]);
}